<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :append-to-body="true" @close="handleClose">
      <el-form label-width="80px" :model="formDate" :rules="rules" ref="ruleForm">
        <el-form-item label="距离远近:" prop="startValue">
            <el-input type="number" v-model="formDate.startValue" size="small" clearable style="width: 20%">
            </el-input>
            ——
            <el-input type="number" v-model="formDate.endValue" size="small" clearable style="width: 20%">
            </el-input>
        </el-form-item>
        <el-form-item label="距离系数:" prop="degreeValue">
          <el-input type="text" placeholder="请输入距离系数" v-model="formDate.degreeValue" size="small" clearable
            style="width: 60%">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addDistanceEditAPI, getDistanceDetailAPI } from "@/api/fms.js";
const defaultListQuery = {
  startValue: "",//开始距离
  endValue: "",//结束距离
  degreeValue: "",//系数值
};
export default {
  name: "addDistanceFactor",
  data() {
    return {
      dialogVisible: false,
      formDate: JSON.parse(JSON.stringify(defaultListQuery)),
      title:"",
      rules: {
        startValue: [
          { required: true, message: '请输入距离远近', trigger: 'blur' },
        ],
        degreeValue: [
          { required: true, message: '请输入距离系数', trigger: 'blur' },
        ],
      }
    };
  },
  created() {
  },
  methods: {
    // 提交
    async btnOk() {
      let res = null
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.formDate.stepId) {
            res = await addDistanceEditAPI(this.formDate);
          } else {
            res = await addDistanceEditAPI(this.formDate);
          }
          if (res) {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          }
          this.handleClose();
          this.$emit("upload");
        } else {
          return false;
        }
      })
    },
    // 回显
    async showDetail(distanceId) {
      this.dialogVisible = true;
      this.title = distanceId ? "修改" : "新增"
      if (distanceId) {
        const res = await getDistanceDetailAPI({
          distanceId: distanceId
        })
        this.formDate = { ...res }
      }
    },
    // 关闭弹框
    handleClose() {
      this.formDate = JSON.parse(JSON.stringify(defaultListQuery))
      this.$refs.ruleForm.resetFields();
      this.dialogVisible = false;
    },
  },
};
</script>
<style lang="scss">
.avatar-uploader-icon {
  margin-bottom: 10px;
  display: inline-block;
  width: 80px;
  height: 80px;
  border: 1px solid #dcdfe6;
  text-align: center;
  line-height: 80px;
  vertical-align: middle;
}
</style>
